﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Video;
using IAlarm;
using ControlLibrary;
using IVideo;

namespace ForestFirePreventionGeography
{
    public partial class FormMain : FormMap
    {
        Dictionary<string, List<string>> devices;
        List<VideoBase> videos = new List<VideoBase>();
        public FormMain()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.LoadMap();

            this.devices = VideoBase.GetAreaDeviceList();

            foreach (KeyValuePair<string, List<string>> area in devices)
            {
                TreeNode TNArea = new TreeNode(area.Key);
                foreach (string device in area.Value)
                {
                    TNArea.Nodes.Add(device);
                }
                tVAreaDevices.Nodes.Add(TNArea);
            }
        }

        protected override void OnMapMove()
        {
            base.OnMapMove();

            foreach (VideoBase video in videos)
            {
                Size size = new Size((int)(this.Times * 352), (int)(this.Times * 288));
                Point location = this.MapToClient(new Point(Convert.ToInt32(video.Longitude), Convert.ToInt32(video.Latitude)));
                location.Offset((int)((352 - size.Width) / 2), (int)((288 - size.Height) / 2));
                video.SetBounds(location.X, location.Y, size.Width, size.Height);
            }
        }

        protected override void OnMapResize()
        {
            base.OnMapResize();

            foreach (VideoBase video in videos)
            {
                Size size = new Size((int)(this.Times * 352), (int)(this.Times * 288));
                Point location = this.MapToClient(new Point(Convert.ToInt32(video.Longitude), Convert.ToInt32(video.Latitude)));
                location.Offset((int)((352 - size.Width) / 2), (int)((288 - size.Height) / 2));
                video.SetBounds(location.X, location.Y, size.Width, size.Height);
            }
        }

        bool isMouseDown = false;
        Point pOffSet = new Point();
        void video_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }

        void video_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown && sender is VideoBase)
            {
                VideoBase video = sender as VideoBase;
                Point pMouse = Cursor.Position;
                pMouse.Offset(pOffSet);
                video.Location = this.PointToClient(pMouse);
            }
        }

        void video_MouseDown(object sender, MouseEventArgs e)
        {
            if (sender is VideoBase)
            {
                VideoBase video = sender as VideoBase;
                video.BringToFront();
                isMouseDown = true;
                pOffSet = new Point(-e.X, -e.Y);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            foreach (var item in videos)
            {
                if (item != null && item.IsOpened) item.CloseVideo();
            }

            foreach (var item in videos)
            {
                if (item != null && item.IsLogined) item.LogoutEncoder();
            }

            base.OnClosing(e);
        }


        private void tVAreaDevices_DoubleClick(object sender, EventArgs e)
        {
            TreeNode TNDevice = tVAreaDevices.SelectedNode;

            if (TNDevice == null || TNDevice.Level != 1 || TNDevice.Text.StartsWith("[不可用]")) return;

            if (TNDevice.Tag == null)
            {//初始化
                UserVideo video = new UserVideo();

                video.Initialize(TNDevice.Parent.Text, TNDevice.Text);

                if (video.LoginEncoder() && video.OpenVideo())
                {
                    video.BorderStyle = BorderStyle.FixedSingle;
                    video.MouseDown += new MouseEventHandler(video_MouseDown);
                    video.MouseMove += new MouseEventHandler(video_MouseMove);
                    video.MouseUp += new MouseEventHandler(video_MouseUp);
                    Size size = new Size((int)(this.Times * 352), (int)(this.Times * 288));
                    Point location = this.MapToClient(new Point(Convert.ToInt32(video.Longitude), Convert.ToInt32(video.Latitude)));
                    location.Offset((int)((352 - size.Width) / 2), (int)((288 - size.Height) / 2));
                    video.SetBounds(location.X, location.Y, size.Width, size.Height);
                    TNDevice.Tag = video;
                    this.Controls.Add(video);
                    videos.Add(video);
                }
                else
                    TNDevice.Text = "[不可用]" + TNDevice.Text;
            }
            else
            {
                VideoBase video = TNDevice.Tag as VideoBase;

                if (video != null && video.IsLogined)
                {
                    if (video.IsOpened) video.CloseVideo();
                    else video.OpenVideo();
                }
            }

        }
    }
}
